package com.asset.salesforce.controller.ajax;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.LeadService;

// TODO: Auto-generated Javadoc
/**
 * The Class ValidatorController.
 */
@Controller
@RequestMapping("/validator/*")
public class ValidatorController {

	/** The Constant logger. */
	private static final Logger logger = Logger.getLogger(ValidatorController.class);
	
	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/** The lead service. */
	@Autowired
	private LeadService leadService;
	
	/** The account service. */
	@Autowired
	private AccountService accountService;
	
	/** The contact service. */
	@Autowired
	private ContactService contactService;

    /**
     * Check mail.
     *
     * @param email the email
     * @return the string
     */
    @RequestMapping(value = "/checkemail", params = "email")
    public @ResponseBody
    String checkMail(@RequestParam String email) {
	logger.debug("checking email : " + email);
		if(employeeService.findEmployeeByEmail(email)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
    
    /**
     * Check company.
     *
     * @param company the company
     * @return the string
     */
    @RequestMapping(value = "/checkcompany", params = "company")
    public @ResponseBody
    String checkCompany(@RequestParam String company) {
	logger.debug("checking company : " + company);
		if(employeeService.findEmployeeByCompany(company)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
    
    /**
     * Check company if company exists.
     *
     * @param company the company
     * @return the string
     */
    @RequestMapping(value = "/checkcompanyexists", params = "company")
    public @ResponseBody
    String checkCompanyIfCompanyExists(@RequestParam String company) {
	logger.debug("checking company : " + company);
		if(employeeService.findEmployeeByCompany(company)==true){
			return "true";
		}
		else{
			return "false";
		}
    }
    
    /**
     * Check lead company exists.
     *
     * @param leadId the lead id
     * @param companyName the company name
     * @return the string
     */
    @RequestMapping(value = "/checkLeadCompanyExists", params = "companyName")
    public @ResponseBody
    String checkLeadCompanyExists(@RequestParam Integer leadId,@RequestParam String companyName) {
	logger.debug("checking company : " + companyName);
		if(leadService.checkCompany(companyName,leadId)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
    
    /**
     * Check phone.
     *
     * @param leadId the lead id
     * @param phone the phone
     * @return the string
     */
    @RequestMapping(value = "/checkPhone", params = "phone")
    public @ResponseBody
    String checkPhone(@RequestParam Integer leadId,@RequestParam String phone) {
	logger.debug("checking phone : " + phone);
		if(leadService.checkPhone(phone,leadId)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
    
    /**
     * Check account company exists.
     *
     * @param accountId the account id
     * @param companyName the company name
     * @return the string
     */
    @RequestMapping(value = "/checkAccountCompanyExists", params = "companyName")
    public @ResponseBody
    String checkAccountCompanyExists(@RequestParam Integer accountId,@RequestParam String companyName) {
	logger.debug("checking company : " + companyName);
		if(accountService.checkCompany(companyName,accountId)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
    
    /**
     * Check account phone.
     *
     * @param accountId the account id
     * @param phone the phone
     * @return the string
     */
    @RequestMapping(value = "/checkAccountPhone", params = "phone")
    public @ResponseBody
    String checkAccountPhone(@RequestParam Integer accountId,@RequestParam String phone) {
	logger.debug("checking phone : " + phone);
		if(accountService.checkPhone(phone,accountId)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
    
    /**
     * Check contact phone.
     *
     * @param contactId the contact id
     * @param phone the phone
     * @return the string
     */
    @RequestMapping(value = "/checkContactPhone", params = "phone")
    public @ResponseBody
    String checkContactPhone(@RequestParam Integer contactId,@RequestParam String phone) {
	logger.debug("checking phone : " + phone);
		if(contactService.checkPhone(phone,contactId)==true){
			return "false";
		}
		else{
			return "true";
		}
    }
}
